<!DOCTYPE html>
<html>
<title>Login Page :: DIF233 - Unit 1 - Excercise 1</title>
<body>
	<?php
		function valid_user($user,$passwd){
			$file = fopen("shadow.txt","a+") or exit("Sorry not able to open the register file");
			while(!feof($file)){
				$line = fgets($file);
				$value = explode(":", $line);
				if(strcmp($value[1], $user) == 0 && strcmp($value[2], $passwd) == 0){
					return 1;
				}
			}
			return 0;
		}
		if(!empty($_POST["submit"])){
			if(valid_user($_POST["user_id"], $_POST["password"])){
				header("Location: user.php?id=".$_POST["user_id"]);
				exit();
			}else{
				echo "<h3>Bad Credentials</h3>";
			}
		}	
	?>
<form name="login" action="login.php" method="post">
		<table border="0" width="100%">
		<tr>
			<td>
				User Name:
			</td>
			<td>
				<input type="text" name="user_id" required/>
			</td>
		</tr>
		<tr>
			<td>
				Password:
			</td>
			<td>
				<input type="password" name="password" required/>
			</td>
		</tr>		
		<tr>
			<td colspan="2"><input type="submit" name="submit" value="submit"/></td>
		</tr>		
	</table>
</form>
</body>
</html>
